home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / os2 / cenv2_19.arj / DELTREE.CMD < prev    next >
OS/2 REXX Batch file  |  1994-03-08  |  5KB  |  110 lines

  1. @echo off
  2. REM *******************************************************************
  3. REM *** DelTree - Delete a subdirectory and all files in it, and    ***
  4. REM *** ver.1     recursively delete all subdirectories within that ***
  5. REM ***           subdirectory.  You may look at DelTree1.cmd and   ***
  6. REM ***           DelTree2.cmd for other methods of doing the       ***
  7. REM ***           same thing by using temporary files.              ***
  8. REM *******************************************************************
  9.    if "%1"=="" GOTO SHOW_HOW
  10.  
  11. REM *******************************************************************
  12. REM *** The user could be very unhappy to delete lots of files they ***
  13. REM *** didn't really want to delete, and so give them a chance to  ***
  14. REM *** change their mind.                                          ***
  15. REM *******************************************************************
  16.    ECHO THIS WILL DELETE ALL FILES AND DIRECTORIES UNDER %1
  17.    CALL GetUKey.cmd "ARE YOU SURE YOU WANT TO DO THIS DELETE ALL THOSE INNOCENT FILES? (Y/N)"    yn
  18.    if N==%UKEY% GOTO FINI
  19.  
  20. REM *********************************************************************
  21. REM *** Check that this is a valid subdirectory.  This can be handled ***
  22. REM *** by calling another CEnvi program: ValidDir.cmd                ***
  23. REM *********************************************************************
  24.    CEnvi ValidDir.cmd %1 COMPLAIN
  25.    if ERRORLEVEL 1 GOTO FINI
  26.  
  27. REM ******************************************************************
  28. REM *** It will be simpler to delete files if we make sure that no ***
  29. REM *** attributes are set that would make them undeletable.  The  ***
  30. REM *** easy way to change this is to let the DOS attrib function  ***
  31. REM *** clear all of these attributes for us.                      ***
  32. REM ******************************************************************
  33.    attrib -H -S -R %1\*.* /s > NUL
  34.  
  35. REM **************************************************************************
  36. REM *** Temporarily turn off DELDIR to make this deletion fast (if unsafe) ***
  37. REM **************************************************************************
  38.    SETLOCAL
  39.    set DELDIR=
  40.  
  41. REM ******************************************************************
  42. REM *** The rest of this task will be handled by CEnvi, which will ***
  43. REM *** create a list of all directories and then call the command ***
  44. REM *** shell to delete the files in each directory.               ***
  45. REM ******************************************************************
  46. CEnvi %0.cmd %1
  47. GOTO CENVI_EXIT
  48.  
  49.    main(argc,argv)
  50.    {
  51.       // build a sorted list of all subdirectories under the specified directory
  52.       DirCount = BuildSubdirList(argv[1],DirList)
  53.       if ( DirCount != 0 ) {
  54.          // sort the subdir list in reverse alphabetic order, which insures that
  55.          // subdirectoies of a directory appear before its parent directory
  56.          qsort(DirList,DirCount,"ReverseSortDirnames")
  57.          // for each subdirectory, call command shell to delete all files in it
  58.          // and then remove the directory
  59.          for ( i = 0; i < DirCount; i++ ) {
  60.             DeleteAllFilesInDir( DirList[i].name );
  61.             RemoveDirectory( DirList[i].name );
  62.          }
  63.       }
  64.       // all subdirectories have have been deleted, then delete the input subdir
  65.       DeleteAllFilesInDir( argv[1] );
  66.       RemoveDirectory( argv[1] );
  67.    }
  68.  
  69.    BuildSubdirList(BaseDir,List) // build List of all subdirectories under BaseDir
  70.    {                             // return count of elements in list
  71.       sprintf(SearchSpec,"%s\\*",BaseDir)
  72.       List = Directory(SearchSpec,TRUE,
  73.                        FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_SUBDIR|FATTR_ARCHIVE,
  74.                        FATTR_SUBDIR)
  75.       return( ( List == NULL ) ? 0 : 1+GetArraySpan(List) );
  76.    }
  77.  
  78.    ReverseSortDirnames(Dir1,Dir2) // repeatedly called by qsort to sort Dir1 and Dir2
  79.    {
  80.       return( stricmp(Dir2.name,Dir1.name) );
  81.    }
  82.  
  83.    DeleteAllFilesInDir(DirName) // call system to delete all files in this dir
  84.    {
  85.       printf("DEL %s\n",DirName)
  86.       system("echo Y | del %s > NUL",DirName)
  87.    }
  88.  
  89.    RemoveDirectory(DirName) // call system RMDIR call
  90.    {
  91.       printf("RMDIR %s\n",DirName)
  92.       system("RMDIR %s > NUL",DirName)
  93.    }
  94.  
  95. :CENVI_EXIT
  96. REM **********************
  97. REM *** Restore DELDIR ***
  98. REM **********************
  99.    ENDLOCAL
  100.    GOTO FINI
  101.  
  102. :SHOW_HOW
  103.    ECHO  
  104.    ECHO DelTree.cmd - Delete a subdirectory and all files and directories under it
  105.    ECHO  
  106.    GOTO FINI
  107.  
  108. :FINI
  109.    SET UKEY=
  110.